package com.springboot.contoller;

import com.springboot.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2022-01-17 16:52
 * @Created by darrren
 */
@Api(value = "用户controller")
@RestController
@RequestMapping("/user")
public class UserController {

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping("/list")
    public List<User> getUserList() {
        List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setId(1l);
        user1.setName("mrbird");
        user1.setAge(25);
        list.add(user1);
        User user2 = new User();
        user2.setId(2l);
        user2.setName("scott");
        user2.setAge(29);
        list.add(user2);
        return list;
    }

    @ApiOperation(value = "增加一个用户",notes = "增加一个用户")
    @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
    @PostMapping("/add")
    public Map<String,Object> addUser(@RequestBody User user){
        if (user != null) {
            System.out.println(user.toString());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("result","success");
        return map;
    }

    @ApiOperation(value = "删除一个用户",notes = "根据id删除用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long",paramType = "path")
    @DeleteMapping("/delete/{id}")
    public Map<String,Object> deleteUser(@PathVariable(value = "id") Long id){
        Map<String,Object> map = new HashMap<>();
        map.put("result","success");
        return map;
    }


    @ApiOperation(value = "更新一个用户",notes = "根据id更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User") })
    @PutMapping("/update/{id}")
    public Map<String,Object> updateUser(@PathVariable(value = "id")Long id,@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("result", "success");
        return map;
    }


    @ApiOperation(value = "获取一个用户",notes = "根据id获取用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long",paramType = "path")
    @GetMapping("/get/{id}")
    public User getUserById(@PathVariable(value = "id")Long id){
        User user = new User();
        user.setId(1l);
        user.setName("mrbird");
        user.setAge(25);
        return user;
    }
}
